var Link = new Class({
	createBox: function(options,edit){
		var _title = '添加链接'; if(edit) _title = '修改链接';
		var box = new PromptBox(
			{relativeTo:options.relativeTo,
			 controls:[{type:'text',title:'链接名称：',id:'lnk_name',valid:'required',cssClass:'inputbox shortinput',tip:'*'},
						{type:'text',title:'链接地址：',id:'lnk_url',valid:'required',cssClass:'inputbox shortinput',tip:'*'}],
			 title:_title,action:function(result){
				var params = String.format('name={0}&url={1}&{2}',
					String.encode(result.lnk_name),String.encode(result.lnk_url),options.extraParams);
				new echoAjax({url:options.url,params:params,onSuccess:function(response){
					if(response.result == true || response.result == 'true'){
						if(response.message == 'noTip') { new MsgBox({message:'操作成功！'}); window.location.reload(); return;}
						else{new MsgBox({message:'你的链接已经提交成功，我们的工作人员稍后会处理，谢谢你的支持！'}); return;} 
					}else{ new MsgBox({message:response.message}); return;}
				}}).fire();
		}});
		return box;
	},
	add: function(options){
		if(options.login){ if(!User.logged_nopay()) { User.login(); return; } }
		this.createBox(options);
	},
	edit: function(options,name,url){
		if(options.login){ if(!User.logged_nopay()) { User.login(); return; } }
		var box = this.createBox(options,true);
		if($chk2(name)) box.getControl('lnk_name').value = name;
		if($chk2(url)) box.getControl('lnk_url').value = url;
	},
	del: function(options){
		if (!User.logged_nopay()) { User.login(); return; }
		var box = new ConfirmBox({message:'你确定要删除这个链接吗?', title:'删除链接',
			action:function(result){
		        new echoAjax({url:options.url, params:options.extraParams, reLoad: true}).fire();
			 }});
	}
});
var Fix = new Class({
	createBox: function(options){
		var _title = '给节目纠错';
		var box = new PromptBox(
			{relativeTo:options.relativeTo,
			 controls:[{type:'textarea',title:'错误描述：',id:'fix_text',valid:'required',cssClass:'shortinput'}],
			 title:_title,action:function(result){
				var params = String.format('content={0}&{1}',String.encode(result.fix_text),options.extraParams);
				new echoAjax({url:options.url,params:params,onSuccess:function(response){
					if(response.result == true || response.result == 'true'){
						new MsgBox({message:'你的纠错信息已经提交成功，我们的工作人员稍后会处理，谢谢你的支持！'}); return;
					}else{ new MsgBox({message:response.message}); return;}
				}}).fire();
		}});
	},
	add: function(options){
		if(options.login){ if(!User.logged()) { User.login(); return; } }
		this.createBox(options);
	}
});
var Watch = new Class({
	hide:function(watch_id){
        new echoAjax({url:"/video/watch/hide/", params:"id=" + watch_id, reLoad: true
        }).fire();
    },
    show:function(watch_id){
        new echoAjax({url:"/video/watch/show/", params:"id=" + watch_id, reLoad: true
        }).fire();
    }

});
var Want = new Class({
    add:function(video_id){
		if (!User.logged_nopay()) { User.login(); return; }
        new echoAjax({url:"/video/want/new/", params:"video_id=" + video_id, reLoad: true
        }).fire();
    },
    hide:function(want_id){
		if (!User.logged_nopay()) { User.login(); return; }
        new echoAjax({url:"/video/want/hide/", params:"id=" + want_id, reLoad: true
        }).fire();
    },
    show:function(want_id){
		if (!User.logged_nopay()) { User.login(); return; }
        new echoAjax({url:"/video/want/show/", params:"id=" + want_id, reLoad: true
        }).fire();
    },
    del:function(want_id){
		if (!User.logged_nopay()) { User.login(); return; }
		var box = new ConfirmBox({message:'你确定要删除这个收藏吗?', title:'删除收藏',
			action:function(result){
		        new echoAjax({url:"/video/want/delete/", params:"id=" + want_id, reLoad: true
		        }).fire();
			 }});
    },
    delByVideo:function(video_id){
		if (!User.logged_nopay()) { User.login(); return; }
		var box = new ConfirmBox({message:'你确定要删除这个收藏吗?', title:'删除收藏',
			action:function(result){
		        new echoAjax({url:"/video/want/delv/", params:"video_id=" + video_id, reLoad: true
		        }).fire();
			}});
    }
	
});
var Favour = new Class({
    add:function(video_id){
		if (!User.logged_nopay()) { User.login(); return; }
        new echoAjax({url:"/user/favour/do/", params:"video_id=" + video_id, reLoad: true
        }).fire();
    },
    del:function(video_id){
		if (!User.logged_nopay()) { User.login(); return; }
		var box = new ConfirmBox({message:'你确定要删除这个收藏吗?', title:'删除收藏',
			action:function(result){
		        new echoAjax({url:"/user/favour/delete/", params:"video_id=" + video_id, reLoad: true
		        }).fire();
			}});
    }
	
});
var Recommend = new Class({
    add:function(video_id,re_load){
		if (!User.logged_nopay()) { User.login(); return; }
		new echoAjax({
			url:'/video/recommend/new/',params:'video_id='+video_id+'&content=',
			onSuccess:function(response){
                if (response.result == "true" || response.result == true) {
					if(re_load) window.location.reload();
					else $('lt_score').load('/video/'+video_id+'/little/score/?' + $random(1,1000));
                } else {
                    new MsgBox({ message: response.message });
                }
			},
			onFailure:function(){ new MsgBox({message:'网络连接超时,请再试一次'}); }
		}).fire();
    },
    del:function(rec_id){
		if (!User.logged_nopay()) { User.login(); return; }
		new ConfirmBox({message:'您确定要删除这个推荐吗？',title:'删除推荐',action:function(){
        	new echoAjax({url:'/video/recommend/delete/', params:'value='+rec_id, reLoad: true}).fire();
		}});
    },
    delMany:function(parent, chkall){
		if (!User.logged_nopay()) { User.login(); return; }
		new ConfirmBox({message:'您确定要删除选中的内容吗？',title:'删除推荐',action:function(){
			var ids = $getCheckValues(parent, chkall);
	        if (!ids) { new MsgBox({ message: "没有选中任何内容" }); return; }
        	new echoAjax({url:'/video/recommend/delete/', params:'value='+ids, reLoad: true}).fire();
		}});
    }
});
var Score = new Class({
    doScore:function(video_id, score){
		if (!User.logged_nopay()) { User.login(); return; }
		var self = this;
		new echoAjax({
			url:'/video/score/new/',params:"video_id=" + video_id + "&score=" + score,
			onSuccess:function(response){
                if (response.result == "true" || response.result == true) {
                    $("lt_score").load("/video/" + video_id + "/little/score/?" + $random(1,1000));
                } else {
                    new MsgBox({ message: response.message });
                }
                self.initTips();
			},
			onFailure:function(){ new MsgBox({message:"网络连接超时, 请再试一次"}); }
		}).fire();
    },
	initTips:function() {
	    var first_rating = $$('.ratingBox .rating .ratingStar')[0];
	    if (first_rating) { this.showTips(first_rating); }
	},
	showTips:function(obj){
	    var obj = $(obj);
	    var rating_text = ['', '烂片', '较差', '还行', '优秀', '精品'];
	    var place = obj.get('tag') == 'ul' ? obj : obj.getParent('ul');
	    var tips = $('rating_tips');
	    if (!tips) {
	        var tips = new Element('span', { 'id': 'rating_tips', 'style': 'padding-left:5px;' });
	        tips.inject(place, 'after');
	    }
	    var from = obj.get('tag') == 'ul' ? (obj.getElements('li')[5] ? obj.getElements('li')[5].innerHTML : 0) : obj.innerHTML;
	    tips.set('text', rating_text[parseInt(from)]);
	}
});
var Book = new Class({
    book:function(video_id){
        new echoAjax({url:'/user/book/new/', params:'video_id=' + video_id, reLoad: true
        }).fire();
    },
    del:function(video_id){
		if (!User.logged_nopay()) { User.login(); return; }
		new ConfirmBox({message:'您确定要删除这个订阅吗？',title:'删除订阅',action:function(){
	        new echoAjax({url:'/user/book/delete/', params:'video_id=' + video_id, reLoad: true
	        }).fire();
		}});
    }
	
});
var Review = new Class({});
var Comment = new Class({});
var Module_Base = new Class({
	Link:null,Fix:null,Watch:null,Want:null,Favour:null,
	Recommend:null,Score:null,
	Book:null,Review:null,Comment:null,
	initialize: function(){
		this.Link = new Link(); this.Fix = new Fix();
		this.Watch = new Watch(); this.Want = new Want();
		this.Favour = new Favour(); 
		this.Recommend = new Recommend(); this.Score = new Score();
		this.Book = new Book();
		this.Review = new Review(); this.Comment = new Comment();
	}
});
var Module = new Module_Base();
